<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
</body>
<script id="ColorVertexShader" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    uniform float u_PointSize;
    uniform mat4 u_ModelM4;
    uniform mat4 u_PvM4;
    varying vec3 v_Posi;
    void main(){
        gl_Position = u_PvM4 * u_ModelM4 * a_Position;
        gl_PointSize = u_PointSize;
        v_Posi = vec3(a_Position.x,a_Position.y,a_Position.z);
    }
</script>
<script id="ColorFragmentShader" type="x-shader/x-fragment">
    precision mediump float;
    uniform vec4 u_Color;
    uniform vec3 u_Center;
    varying vec3 v_Posi;
    void main(){
        float dist = distance(v_Posi, u_Center);
        if(dist < 0.5){
            gl_FragColor = u_Color;
        }
    }
</script>
<script type="module">
    import { Matrix4, Vector3, Color, OrthographicCamera, PerspectiveCamera, Spherical } from '../js/three.module.js';
    import { CreateProgram, imgPromise } from '../js/Utils.js';
    import lib from '../js/ThreeLib.js';
    import CameraController from '../js/CameraController.js'
    import Poly from '../js/Poly4.js'
    import Control from '../js/Control.js'

    import Geo from '../js/Poly2/Geo.js'
    import Mat from '../js/Poly2/Mat.js'
    import Obj3D from '../js/Poly2/Obj3D.js'
    import Scene from '../js/Poly2/Scene.js'



    //#region gl
    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight - 4;

    const gl = canvas.getContext("webgl", { antialias: true, });

    // gl.enable(gl.DEPTH_TEST);//开始背景遮挡会频闪
    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);//去图片描边
    //#endregion



    const scene = new Scene({ gl: gl });
    scene.regProgram('color', {
        program: CreateProgram(gl,
            document.querySelector('#ColorVertexShader').innerText,
            document.querySelector('#ColorFragmentShader').innerText),
        attribs: ['a_Position'],
        uniforms: ['u_PvM4', 'u_ModelM4', 'u_PointSize', 'u_Color', 'u_Center']
    })


    const pvM4 = new Matrix4();
    const modelM4 = new Matrix4();

    const camera = new PerspectiveCamera(45, canvas.width / canvas.height, 1, 20);
    const cameraController = new CameraController({
        canvas,
        camera,
        pvMatrix: pvM4,
        rendEvent: () => { scene.draw(); },
    })

    const nodeLength = 0.5;
    const nodeArr = [
        -nodeLength, nodeLength, 0,
        -nodeLength, -nodeLength, 0,
        nodeLength, -nodeLength, 0,
        nodeLength, nodeLength, 0,
    ];

    const elementArr = [
        0, 1, 2,
        0, 2, 3,
    ];

    const nodeArr3D = [
        -nodeLength, 0, -nodeLength,
        -nodeLength, 0, nodeLength,
        nodeLength, 0, nodeLength,
        nodeLength, 0, -nodeLength,
        0, nodeLength, nodeLength,
        0, -nodeLength, nodeLength,
        0, -nodeLength, -nodeLength,
        0, nodeLength, -nodeLength,
        -nodeLength, nodeLength, 0,
        -nodeLength, -nodeLength, 0,
        nodeLength, -nodeLength, 0,
        nodeLength, nodeLength, 0,
    ];

    const elementArr3D = [
        0, 1, 2, 0, 2, 3,
        4, 5, 6, 4, 6, 7,
        8, 9, 10, 8, 10, 11,
    ];

    //main


    // scene.add(create2D());
    scene.add(create3D());//3D的应该特殊处理,也不只是这样的哈哈哈
    scene.draw();

    function create3D() {
        const center = new Vector3(0, 0, 0);
        return new Obj3D({
            geo: new Geo({
                data: {
                    'a_Position': { size: 3, array: new Float32Array(nodeArr3D) },
                },
                element: {
                    array: new Uint8Array(elementArr3D)
                }
            }),
            mat: new Mat({
                program: 'color',
                mode: ['TRIANGLES'],
                data: {
                    'u_PvM4': { type: 'uniformMatrix4fv', value: pvM4.elements },
                    'u_ModelM4': { type: 'uniformMatrix4fv', value: modelM4.elements },
                    'u_PointSize': { type: 'uniform1f', value: 30 },
                    'u_Color': { type: 'uniform4fv', value: [1, 1, 1, 1] },
                    'u_Center': { type: 'uniform3fv', value: [center.x, center.y, center.z] },
                },
            }),
        });
    }

    function create2D() {
        const center = getCenterOf2D(nodeArr);
        return new Obj3D({
            geo: new Geo({
                data: {
                    'a_Position': { size: 3, array: new Float32Array(nodeArr) },
                },
                element: {
                    array: new Uint8Array(elementArr)
                }
            }),
            mat: new Mat({
                program: 'color',
                mode: ['TRIANGLES'],
                data: {
                    'u_PvM4': { type: 'uniformMatrix4fv', value: pvM4.elements },
                    'u_ModelM4': { type: 'uniformMatrix4fv', value: modelM4.elements },
                    'u_PointSize': { type: 'uniform1f', value: 30 },
                    'u_Color': { type: 'uniform4fv', value: [1, 1, 1, 1] },
                    'u_Center': { type: 'uniform3fv', value: [center.x, center.y, center.z] },
                },
            }),
        });
    }

    function getCenterOf3D(array) {
        let a = new Vector3(array[0], array[1], array[2]);
        let b = new Vector3(array[3 * 6], array[3 * 6 + 1], array[3 * 6 + 2]);
        return b.clone().sub(a).multiplyScalar(0.5).add(a);
    }

    function getCenterOf2D(array) {
        let a = new Vector3(array[0], array[1], array[2]);
        let b = new Vector3(array[3 * 2], array[3 * 2 + 1], array[3 * 2 + 2]);
        return b.clone().sub(a).multiplyScalar(0.5).add(a);
    }
</script>

</html>